import { Controller, Get, Post, Put, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { ConfigService } from './config.service';
import { SiteConfig } from './config.entity';
import { JwtHttpGuard } from '../auth/jwt-http.guard';

@Controller('config')
@UseGuards(JwtHttpGuard)
export class ConfigController {
  constructor(private readonly configService: ConfigService) {}

  @Get('list')
  async findAll(): Promise<SiteConfig[]> {
    return await this.configService.findAll();
  }

  @Get(':id')
  async findOne(@Param('id') id: string): Promise<SiteConfig | null> {
    return await this.configService.findOne(+id);
  }

  @Get('key/:key')
  async findByKey(@Param('key') key: string): Promise<SiteConfig | null> {
    return await this.configService.findByKey(key);
  }

  @Post()
  async create(@Body() configData: Partial<SiteConfig>): Promise<SiteConfig> {
    return await this.configService.create(configData);
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() configData: Partial<SiteConfig>): Promise<SiteConfig | null> {
    return await this.configService.update(+id, configData);
  }

  @Delete(':id')
  async remove(@Param('id') id: string): Promise<void> {
    return await this.configService.remove(+id);
  }

  @Post('batch')
  async batchUpdate(@Body('configs') configs: { key: string; value: string }[]): Promise<void> {
    return await this.configService.batchUpdate(configs);
  }

  @Get('value/:key')
  async getConfigValue(
    @Param('key') key: string,
    @Body('defaultValue') defaultValue: string = '',
  ): Promise<string> {
    return await this.configService.getConfigValue(key, defaultValue);
  }
}